﻿using AutoMapper;
using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.ReadAPI.Applications.Commands.Approves;
using B.S.Meeting.ReadAPI.Dtos;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Meeting.ReadAPI.Applications.CommandHandlers.Approves
{
    public class GetApproveCommandHandler : IRequestHandler<GetApproveCommand, APIResult<APIPaging<List<ApproveDto>>>>
    {
        IBaseRepository<ApproveReservation> aRep;
        IBaseRepository<MeetingReservation> mrRep;
        IBaseRepository<MeetingRoom> rRep;
        IMapper mapper;
        ILogger<GetApproveCommandHandler> logger;

        public GetApproveCommandHandler(IBaseRepository<ApproveReservation> aRep, IBaseRepository<MeetingReservation> mrRep, IBaseRepository<MeetingRoom> rRep, IMapper mapper, ILogger<GetApproveCommandHandler> logger)
        {
            this.aRep = aRep;
            this.mrRep = mrRep;
            this.rRep = rRep;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<APIResult<APIPaging<List<ApproveDto>>>> Handle(GetApproveCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var alist = aRep.GetAll();
                var mrlist= mrRep.GetAll().Where(x=>!x.IsDel);
                var rlist=rRep.GetAll().Where(x=>!x.IsDel);
                if (!string.IsNullOrEmpty(request.ThemeName)) 
                {
                    mrlist=mrlist.Where(x=>x.ThemeName.Contains(request.ThemeName));
                }
                if (!string.IsNullOrEmpty(request.Location)) 
                {
                    rlist= rlist.Where(x=>x.Location.Contains(request.Location));
                }
                if (!string.IsNullOrEmpty(request.MeetingDate))
                {
                    mrlist = mrlist.Where(x => x.MeetingDate >= DateTime.Parse(request.MeetingDate));
                }
                //if (!string.IsNullOrEmpty(request.MeetingDate))
                //{
                //    mrlist = mrlist.Where(x => x.MeetingDate < DateTime.Parse(request.MeetingDate).AddDays(1));
                //}
                if (request.ApproveReservationStatus!=null)
                {
                    alist = alist.Where(x=>x.ApproveReservationStatus == request.ApproveReservationStatus);
                }
                var list=from a in alist
                         join mr in mrlist
                         on a.MeetingReservationId equals mr.MeetingReservationId
                         join r in rlist
                         on mr.MeetingRoomId equals r.MeetingRoomId
                         select a;
                var totalCount= list.Count();
                var totalPage=(int)Math.Ceiling(totalCount*1.0/request.PageSize);
                list=list.OrderBy(x=>x.ApproveReservationId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize);
                var aDto = mapper.Map<List<ApproveDto>>(list);
                List<MeetingReservation> mrlists=mrRep.GetAll().ToList();
                List<MeetingRoom> rlists = rRep.GetAll().ToList();
                foreach (var item in aDto)
                {
                    item.TemplateName = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).TemplateName;
                    item.ThemeName = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).ThemeName;
                    item.MeetingRoomId = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).MeetingRoomId;
                    item.HostName = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).HostName;
                    item.MeetingDate = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).MeetingDate;
                    item.BegingTime = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).BegingTime;
                    item.EndTime = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).EndTime;
                    item.JoinName = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).JoinName;
                    item.Content = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).Content;
                    item.IsRegister = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).IsRegister;
                    item.IsPassword = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).IsPassword;
                    item.PasswordMessage = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).PasswordMessage;
                    item.IsNotice = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).IsNotice;
                    item.Teleconference = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).Teleconference;
                    item.ReservationStatus = mrlists.Find(x => x.MeetingReservationId == item.MeetingReservationId).ReservationStatus;
                    item.Location = rlists.Find(x => x.MeetingRoomId == item.MeetingRoomId).Location;
                }
                return new APIResult<APIPaging<List<ApproveDto>>>
                {
                    Code=StatusEnumCode.Ok,
                    Message="获取审批列表成功",
                    Data=new APIPaging<List<ApproveDto>>
                    {
                        TotalCount=totalCount,
                        TotalPage=totalPage,
                        Data=aDto
                    }
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
